package info.niwota.fwrite;

import java.io.Serializable;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;


public abstract class Element implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public static final String KEY_ELEMENT = "e";
	
	public static final int ELEMENT_BLANK = 0;
	public static final int ELEMENT_TEXT = 1;
	public static final int ELEMENT_GESTURE = 2;
	public static final int ELEMENT_IMAGE = 3;
	public static final int ELEMENT_RESOURCE_IMAGE = 4;
	
	public static final String KEY_WIDTH = "w";

	public static final String KEY_HEIGHT = "h";

	public static final String KEY_INSET = "i";

	public static final String KEY_COLOR = "c";
	
	public static final String KEY_SIZE = "s";
	
	public static final String KEY_TEXT = "t";
	
	public static final String KEY_TYPE = "tt";
	
	public static final String KEY_BITMAP = "bm";

	public static final String KEY_PATH= "p";

	public int color;
	public int height;
	public int inset;
	public int width;
	
	public transient Context ctx;

	public Element() {
		super();
	}

	public void setContext(Context ctx) {
		this.ctx = ctx;
	}
	
	public Element(int w, int h, int inset, int color) {
		this.width = w;
		this.height = h;
		this.inset = inset;
		this.color = color;
	}

	public int getColor() {
		return color;
	}

	public int getHeight() {
		return height;
	}

	public int getInset() {
		return inset;
	}

	public int getWidth() {
		return width;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setInset(int inset) {
		this.inset = inset;
	}

	public void setWidth(int width) {
		this.width = width;
	}
	
	/**
	 * recompute width and height
	 */
	public void recompute(Paint paint) {
	}
	
	public Element(JSONObject obj) throws Exception {
		this.width = obj.getInt(KEY_WIDTH);
		this.height = obj.getInt(KEY_HEIGHT);
		this.inset = obj.getInt(KEY_INSET);
		this.color = obj.getInt(KEY_COLOR);
	}
	
	public static Element toElement(JSONObject obj) throws Exception{
		Element el = null;
		int type = obj.getInt(KEY_ELEMENT);
		switch(type) {
		case ELEMENT_BLANK:
			el = new BlankElement(obj);
			break;
		case ELEMENT_GESTURE:
			el = new GestureElement(obj);
			break;
		case ELEMENT_IMAGE:
			el = new ImageElement(obj);
			break;
		case ELEMENT_RESOURCE_IMAGE:
			el = new ResourceImageElement(obj);
			break;
		case ELEMENT_TEXT:
			el = new TextElement(obj);
			break;
		}
		return el;
	}
	
	public abstract JSONObject toJSON() throws Exception;
	
	protected JSONObject toJSON(int type) throws Exception {
		JSONObject obj = new JSONObject();
		obj.put(KEY_ELEMENT, type);
		
		obj.put(KEY_COLOR, color);
		obj.put(KEY_INSET, inset);
		obj.put(KEY_HEIGHT, height);
		obj.put(KEY_WIDTH, width);
		
		return obj;
	}
	
	public abstract void draw(Canvas canvas, Paint paint, float x, float y, float height, float lh);
}